import React from 'react';
import PropTypes from 'prop-types';//react 16.X这样用，改动太大，升级困难

class Year extends React.Component {
    constructor(props) {
        super(props);
    }

    static propTypes = {
        begin: PropTypes.string.isRequired,
        end: PropTypes.string.isRequired,
    }

    static defaultProps = {
        year: '',
    }

    state = {
        arr: new Array(12),//这样设置，react是不会渲染的，因为内容为空
    }

    initState(begin, end) {
        const arr = [];
        for (let i = begin; i <= end; i++) {
            arr.push(i)
        }
        this.setState({
            arr
        })
    }

    componentWillMount() {
        let begin = this.props.begin - 0;
        let end = this.props.end - 0;
        if (begin > end) {
            begin = end;
            end = this.props.begin - 0;
        }
        this.initState(begin, end);
    }

    onChange = (e) => {
        const value = e.target.value;
        if (value === this.props.month) {
            return;
        }
        this.props.onChange(value);
    }

    render() {
        const { color, text } = this.props;

        return (<select onChange={this.onChange} value={this.props.year}>
            {
                this.state.arr.map((item, index, arr) => {
                    return (<option key={index} value={item}>{item}年</option>)
                })
            }
        </select>);
    }
}

export default Year;
